package com.ktgj.flight.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.flight.domain.FlightOrderInfoTicketNo;
import com.ktgj.flight.mapper.FlightOrderInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightOrderInfoTicketsMapper;
import com.ktgj.flight.domain.FlightOrderInfoTickets;
import com.ktgj.flight.service.IFlightOrderInfoTicketsService;

/**
 * 机票预订_机票订单机票明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class FlightOrderInfoTicketsServiceImpl implements IFlightOrderInfoTicketsService 
{
    @Autowired
    private FlightOrderInfoTicketsMapper flightOrderInfoTicketsMapper;

    @Autowired
    private FlightOrderInfoMapper flightOrderInfoMapper;

    /**
     * 查询机票预订_机票订单机票明细
     * 
     * @param orderInfoTicketsId 机票预订_机票订单机票明细ID
     * @return 机票预订_机票订单机票明细
     */
    @Override
    public FlightOrderInfoTickets selectFlightOrderInfoTicketsById(Long orderInfoTicketsId)
    {
        return flightOrderInfoTicketsMapper.selectFlightOrderInfoTicketsById(orderInfoTicketsId);
    }

    /**
     * 查询机票预订_机票订单机票明细列表
     * 
     * @param flightOrderInfoTickets 机票预订_机票订单机票明细
     * @return 机票预订_机票订单机票明细
     */
    @Override
    public List<FlightOrderInfoTickets> selectFlightOrderInfoTicketsList(FlightOrderInfoTickets flightOrderInfoTickets)
    {
        return flightOrderInfoTicketsMapper.selectFlightOrderInfoTicketsList(flightOrderInfoTickets);
    }

    /**
     * 查询机票预订_机票订单机票明细
     *
     * @param orderId 机票预订_机票订单机票明细ID
     * @return 机票预订_机票订单机票明细
     */
    @Override
    public List<FlightOrderInfoTickets> selectFlightOrderInfoTicketsByOrderId(Long orderId) {
        return flightOrderInfoTicketsMapper.selectFlightOrderInfoTicketsByOrderId(orderId);
    }

    /**
     * 新增机票预订_机票订单机票明细
     * 
     * @param flightOrderInfoTickets 机票预订_机票订单机票明细
     * @return 结果
     */
    @Override
    public int insertFlightOrderInfoTickets(FlightOrderInfoTickets flightOrderInfoTickets)
    {
        return flightOrderInfoTicketsMapper.insertFlightOrderInfoTickets(flightOrderInfoTickets);
    }

    /**
     * 修改机票预订_机票订单机票明细
     * 
     * @param flightOrderInfoTickets 机票预订_机票订单机票明细
     * @return 结果
     */
    @Override
    public int updateFlightOrderInfoTickets(FlightOrderInfoTickets flightOrderInfoTickets)
    {
        return flightOrderInfoTicketsMapper.updateFlightOrderInfoTickets(flightOrderInfoTickets);
    }

    /**
     * 修改票号
     *
     * @return 结果
     */
    @Override
    public int updateFlightOrderInfoTicketsByTicketNo(FlightOrderInfoTicketNo ticketNo) {
        for (FlightOrderInfoTickets infoTicket : ticketNo.getInfoTickets()) {
            int no = flightOrderInfoTicketsMapper.updateFlightOrderInfoTicketsByTicketNo(infoTicket);
            if (no == 0) {
                return 0;
            }
        }
        return 1;
    }

    /**
     * 批量删除机票预订_机票订单机票明细
     * 
     * @param orderInfoTicketsIds 需要删除的机票预订_机票订单机票明细ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderInfoTicketsByIds(Long[] orderInfoTicketsIds)
    {
        return flightOrderInfoTicketsMapper.deleteFlightOrderInfoTicketsByIds(orderInfoTicketsIds);
    }

    /**
     * 删除机票预订_机票订单机票明细信息
     * 
     * @param orderInfoTicketsId 机票预订_机票订单机票明细ID
     * @return 结果
     */
    @Override
    public int deleteFlightOrderInfoTicketsById(Long orderInfoTicketsId)
    {
        return flightOrderInfoTicketsMapper.deleteFlightOrderInfoTicketsById(orderInfoTicketsId);
    }

    @Override
    public Map<String, Object> queryFlightOrderTrip(Long orderID) {
        List<FlightOrderInfoTickets> flightOrderInfoTickets = flightOrderInfoTicketsMapper.queryFlightOrderTrip(orderID);
        Map<String, Object> map = new HashMap<>();
        for (FlightOrderInfoTickets flightOrderInfoTicket : flightOrderInfoTickets) {
            map.put("orderInfoTicketsId", flightOrderInfoTicket.getOrderInfoTicketsId());
            map.put("depTerm", flightOrderInfoTicket.getDepTerm());
            map.put("airliner", flightOrderInfoTicket.getAirliner());
            map.put("flight", flightOrderInfoTicket.getFlight());
            map.put("arrTerm", flightOrderInfoTicket.getArrTerm());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String departureTime = dateFormat.format(flightOrderInfoTicket.getDepartureTime());
            map.put("departureTime", departureTime.split(" ")[1]);
            String arrivalTime = dateFormat.format(flightOrderInfoTicket.getArrivalTime());
            map.put("arrivalTime", arrivalTime.split(" ")[1]);
            map.put("tripMeal", flightOrderInfoTicket.getTripMeal());
            map.put("flightTimes", "2小时15分");
            map.put("checkCounter", "F021");
            map.put("boardinGgate", "43");
        }
        return map;
    }

    /**
     * 根据订单ID获取订单详细信息
     * @param orderId
     * @return
     */
    @Override
    public List<FlightOrderInfoTickets> selectOrderInfoTicketByOrderId(Long orderId) {
        return flightOrderInfoTicketsMapper.selectOrderInfoTicketByOrderId(orderId);
    }

    /**
     * 根据票号获取订单详细信息
     * @param ticketNo
     * @return
     */
    @Override
    public FlightOrderInfoTickets getFlightOrderInfoByTicketNo(String ticketNo) {
        return flightOrderInfoTicketsMapper.getFlightOrderInfoByTicketNo(ticketNo);
    }

    @Override
    public List<FlightOrderInfoTickets> getFlightOrderInfoByTicketNoList(String ticketNo) {
        return flightOrderInfoTicketsMapper.getFlightOrderInfoByTicketNoList(ticketNo);
    }

    @Override
    public Map<String, Object> queryInfoTickets(Long customerId) {
        Map<String, Object> map = new HashMap<>();
        FlightOrderInfoTickets tickets = flightOrderInfoTicketsMapper.queryInfoTickets(customerId, CustomerConstants.waitPay.getValue());
        map.put("airliner", tickets.getAirliner());
        map.put("flight", tickets.getFlight());
        map.put("flightType", tickets.getFlightType());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (tickets.getDepartureTime() != null) {
            map.put("departureTime", dateFormat.format(tickets.getDepartureTime()));
        }
        if (tickets.getArrivalTime() != null) {
            map.put("arrivalTime", dateFormat.format(tickets.getArrivalTime()));
        }
        map.put("depTerm", tickets.getDepTerm());
        map.put("arrTerm", tickets.getArrTerm());
        map.put("tripMeal", tickets.getTripMeal());
        return map;
    }

    @Override
    public List<FlightOrderInfoTickets> selectRescheduled(Long orderId) {
        return flightOrderInfoTicketsMapper.selectRescheduled(orderId);
    }

    @Override
    public int updateFlightOrderInfoTicket(FlightOrderInfoTickets flightOrderInfoTickets) {
        return flightOrderInfoTicketsMapper.updateFlightOrderInfoTicket(flightOrderInfoTickets);
    }

    @Override
    public int updateFlightOrderInfoTicketsByOrderId(Long orderId, String status) {
        return flightOrderInfoTicketsMapper.updateFlightOrderInfoTicketsByOrderId(orderId, status);
    }

    //机票分析
    @Override
    public List<FlightOrderInfoTickets> flightAnalysis(Long customerId, String startDate, String endDate) {
        return flightOrderInfoTicketsMapper.flightAnalysis(customerId, startDate, endDate);
    }

    /**
     * 回填票号
     * */
    @Override
    public int backfillTicketNumber(Long id, String ticketNumber) {
        int i = flightOrderInfoTicketsMapper.backfillTicketNumber(id, ticketNumber);
        return i;
    }

    /**
     * 订单号获取乘客id列表
     * */
    @Override
    public List<Long> flightOrderIdList(Long orderId) {
        List<Long> idList = new ArrayList<>();
        List<FlightOrderInfoTickets> flightOrderInfoTickets = flightOrderInfoTicketsMapper.checkTheTicketList(orderId);
        if (flightOrderInfoTickets.size() > 0){
            for (FlightOrderInfoTickets flightOrderInfoTicket : flightOrderInfoTickets) {
                idList.add(flightOrderInfoTicket.getOrderInfoTicketsId());
            }
        }
        return idList;
    }

    /**
     * 机票订单详细（远程）
     * */
    @Override
    public boolean order(String ticketNum) {
        boolean flag = false;
        FlightOrderInfoTickets order = flightOrderInfoTicketsMapper.order(ticketNum);
        if (order != null){
            flag = true;
        }
        return flag;
    }
}
